<?php
namespace App\Model;

class Admin extends Model
{
    use \HyperfSanctum\Tokens;
    
    // 表名
    protected $table = 'admin';
    
    // 保护属性
    protected $guarded = [];
    
    protected $attributes = [
        'daili_id' => 0
    ];
    
    protected $hidden = [
        'password',
    ];
    
    protected $dateFormat = 'U';
    
    // 创建事件
    public function creating()
    {
        // 密码加密
        $this->password = password_hash((string) $this->password, PASSWORD_DEFAULT);
    }
    
    // 更新事件
    public function updating()
    {
        // 密码发现变动
        if(password_needs_rehash((string) $this->password, PASSWORD_DEFAULT))
        {
            $this->creating();
        }
    }
    
    // 代理
    public function daili()
    {
        return $this->belongsTo(Daili::class, 'daili_id');
    }
}